package javawdw.io.simple;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javawdw.io.BinaryWriter;

/**
 * 二进制文件写入器
 * 
 * @author David
 * 
 */
public class BufferedBinaryWriter implements BinaryWriter {
	private OutputStream writer;
	private OutputStream outputStream;

	/**
	 * 根据给定的文件创建写入器
	 * 
	 * @param file
	 *            给定的文件
	 * @param type
	 *            写入器类型
	 */
	public BufferedBinaryWriter(OutputStream outputStream) {
		writer = new BufferedOutputStream(outputStream);
	}

	/**
	 * 将给定的字节数组写入文件或流中
	 * 
	 * @param buf
	 */
	public void write(byte[] buf) {
		try {
			writer.write(buf);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 刷新写入器
	 */
	public void flush() {
		try {
			outputStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭写入器
	 */
	public void close() {
		try {
			this.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}